/*
 * mod_user.c
 *
 *  Created on: Mar 4, 2012
 *      Author: Administrator
 */
#include "user.h"
#include "utils.h"
#include "dictionary.h"
#include "iniparser.h"
#include "length_define.h"
#include "xmppconnect.h"
#ifndef CONFIG_FILE_PATH
	#define CONFIG_FILE_PATH_QUOTEED "/etc/sipxmppserver.conf"
	#error CONFIG_FILE_PATH not defined.
#else
	#define CONFIG_FILE_PATH_QUOTEED QUOTEME_FUNC(CONFIG_FILE_PATH)
#endif

static sip_user_t *sip_users = NULL;

static int xmpp_2_sip_mod_user_config_find_user_by_sip_user_name(char *sip_username,struct _sip_user_ **user);
static int load_sip_users();
static void free_sip_users();
static struct user_manager xmpp_2_sip_user_manager = {
		.find_user_by_sip_user_name = xmpp_2_sip_mod_user_config_find_user_by_sip_user_name,
};
static int load_sip_users()
{
	char tmp_key[255],*sip_username;
	dictionary	*config_dictionary = iniparser_load(CONFIG_FILE_PATH_QUOTEED);
	if(!config_dictionary)
	{
		Log("Load config file %s error.",CONFIG_FILE_PATH_QUOTEED);
		return 1;
	}
	int section_count = iniparser_getnsec(config_dictionary);
	if(section_count == 0)
	{
		Log("no user found in config file %s",CONFIG_FILE_PATH_QUOTEED);
		return 1;
	}
	int i;
	sip_user_t *user,*tmp_user = NULL,*delete_user = NULL;
	char renewuser = 0;
	xmpp_connection_t *xmpp_connection = NULL;
	for(i = 0 ; i < section_count ;i++)
	{
		Log("section[%d]",i);
		sip_username = iniparser_getsecname(config_dictionary,i);
		if(xmpp_2_sip_find_user_by_sip_user_name(sip_username,&user) || !user)
		{
			user = sipxmpp_calloc(sizeof(struct _sip_user_));
			renewuser = 0;
		}
		else
			renewuser = 1;
		strcpy(user->sip_username,iniparser_getsecname(config_dictionary,i));
		sprintf(tmp_key,"%s:%s",sip_username,"password");
		strcpy(user->sip_password,iniparser_getstring(config_dictionary,tmp_key,""));
		sprintf(tmp_key,"%s:%s",sip_username,"xmpp_username");
		strncpy(user->xmpp_username,iniparser_getstring(config_dictionary,tmp_key,""),128);
		sprintf(tmp_key,"%s:%s",sip_username,"xmpp_password");
		strncpy(user->xmpp_password,iniparser_getstring(config_dictionary,tmp_key,""),128);

		if(!sipxmpp_find_connection_by_sip_username(user->xmpp_username,&xmpp_connection) && xmpp_connection)
		{
			strncpy(xmpp_connection->username,user->xmpp_username,128);
			strncpy(xmpp_connection->password,user->xmpp_password,128);
			if(xmpp_connection->status == SIPXMPP_CONNECTION_STATUS_ERROR)
				xmpp_connection->status = SIPXMPP_CONNECTION_STATUS_DISCONNECTED;
		}
		user->delete_flag = 1;
		if(renewuser) continue;
		if(!sip_users)
		{
			sip_users = user;
		}
		else
		{
			tmp_user = sip_users;
			while(tmp_user->next)
			{
				tmp_user = tmp_user->next;
			}
			tmp_user->next = user;
		}
	}
	//remove deleted users [which delete_flag is not set.]
	remove_deleted_users(&sip_users);
	iniparser_freedict(config_dictionary);
	return 0;
}
static void free_sip_users()
{
	struct _sip_user_ *tmp_user;
	LogD("begin free sip_users");
	while(sip_users)
	{
		LogD("freeing sip_users");
		tmp_user = sip_users->next;
		chkndfree(sip_users);
		sip_users = tmp_user;
	}
}
int mod_init(){
	xmpp_2_sip_set_user_manager(&xmpp_2_sip_user_manager);
	load_sip_users();
	return 0;
}
int mod_unload(){
	free_sip_users();
	return 0;
}
int mod_reload(){
	LogD("reload module.");
	load_sip_users();
	return 0;
}
static int xmpp_2_sip_mod_user_config_find_user_by_sip_user_name(char *sip_username,struct _sip_user_ **user)
{
	struct _sip_user_ *tmp_user = sip_users;
	while(tmp_user != NULL)
	{
		if(!strncmp(sip_username,tmp_user->sip_username,SIP_USERNAME_LEN))
		{
			if(user != NULL) *user = tmp_user;
			return S_OK;
		}
		tmp_user = tmp_user->next;
	}
	return S_ERROR;
}
